package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.behavior.mapper.ApUnlikeBehaviorMapper;
import com.heima.behavior.service.ApArticleRelationService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.behavior.service.ApUnlikesBehaviorService;
import com.heima.model.behavior.dto.ApArticleRelationDto;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApLikesBehavior;
import com.heima.model.behavior.pojo.ApUnlikesBehavior;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;



@Service
public class ApArticleRelationServiceImpl implements ApArticleRelationService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private ApUnlikesBehaviorService apUnlikesBehaviorService;
    @Autowired
    private ApLikesBehaviorService apLikesBehaviorService;
    @Override
    public Map findApArticleRelation(ApArticleRelationDto dto) {
        //1.检查参数
        Map resultMap = new HashMap<>();
        resultMap.put("islike",false);
        resultMap.put("isunlike",false);
        if (dto == null || dto.getArticleId() ==null || dto.getEntryId() ==null) {
            return resultMap;
        }
        //2.查询实体
        ApBehaviorEntry apBehaviorEntry =null;
        if (dto.getType()==1){
            apBehaviorEntry= apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getEntryId(), null);
        }else{
            apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(null,dto.getEntryId());
        }
        if (apBehaviorEntry ==null){
            return resultMap;
        }
        resultMap.put("entryId",apBehaviorEntry.getId());
        //3.根据实体查询行为
        //3.1查询文章不喜欢
        ApUnlikesBehavior apUnlikesBehavior = this.apUnlikesBehaviorService.getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery()
                .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApUnlikesBehavior::getEntryId, apBehaviorEntry.getId()));
        if (apUnlikesBehavior != null && apUnlikesBehavior.getType().intValue() == 0) {
            resultMap.put("isunlike", true);
        }
        //3.1查询文章点赞
        ApLikesBehavior apLikesBehavior = this.apLikesBehaviorService.getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId()));
        if (apLikesBehavior !=null && apLikesBehavior.getType().intValue() ==0){
            resultMap.put("islike",true);
        }
        //封装
        return resultMap;
    }
}


